package com.chainyoung.component_tmperson.ui.fragment;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chainyoung.common.appcommon.entity.HdWalletTransaction;
import com.chainyoung.common.base.BaseFragment;
import com.chainyoung.common.custom_widget.EmptyCallback;
import com.chainyoung.common.custom_widget.RetryCallback;
import com.chainyoung.common.di.AppComponent;
import com.chainyoung.component_tmperson.R;
import com.chainyoung.component_tmperson.R2;
import com.chainyoung.component_tmperson.di.component.DaggerPersonFragmentComponent;
import com.chainyoung.component_tmperson.di.module.PersonFragmentModule;
import com.chainyoung.component_tmperson.ui.adapter.HdTransactionListAdapter;
import com.chainyoung.component_tmperson.ui.mvp.contract.HdWalletContract;
import com.chainyoung.component_tmperson.ui.mvp.presenter.HdWalletPresenter;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * 本地钱包转账记录
 */
public class HdWalletTransactionListFragment extends BaseFragment<HdWalletPresenter> implements HdWalletContract.View {


    @BindView(R2.id.rv_list)
    RecyclerView rvList;
    @BindView(R2.id.refresh_layout)
    SmartRefreshLayout refreshLayout;

    private String address;//钱包地址


    private List<HdWalletTransaction> mData = new ArrayList<>();
    private HdTransactionListAdapter adapter;

    public static HdWalletTransactionListFragment newInstance(String address) {
        HdWalletTransactionListFragment fragment = new HdWalletTransactionListFragment();
        Bundle args = new Bundle();
        args.putString("address", address);
        fragment.setArguments(args);
        return fragment;
    }


    @Override
    protected void setupFragmentComponent(AppComponent appComponent) {
        DaggerPersonFragmentComponent.builder()
                .appComponent(appComponent)
                .personFragmentModule(new PersonFragmentModule(this))
                .build()
                .inject(this);
    }


    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        if (getArguments() != null) {
            address = getArguments().getString("address");
        }
    }

    @Override
    protected void initUi() {
        super.initUi();
        initRecylerView();
    }

    private void initRecylerView() {
        adapter = new HdTransactionListAdapter(mData, mContext, address);
        DividerItemDecoration itemDecoration = new DividerItemDecoration(mContext, DividerItemDecoration.VERTICAL);
        itemDecoration.setDrawable(ContextCompat.getDrawable(mContext, R.drawable.shape_divider_gray));
        rvList.addItemDecoration(itemDecoration);
        rvList.setLayoutManager(new LinearLayoutManager(mContext));
        rvList.setAdapter(adapter);
        refreshLayout.setEnableLoadMore(false);
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                loadData();
            }
        });
    }

    @Override
    protected boolean useLoadSir() {
        return true;
    }

    @Override
    protected void onPageRetry(View v) {
        super.onPageRetry(v);
        loadData();
    }

    @Override
    protected void initData() {

    }


    @Override
    protected void onFirstUserVisible() {
        loadData();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.tmperson_fragment_hdwallet_token_list;
    }

    private void loadData() {
        mPresenter.getTransactions(address);
    }


    @Override
    public void onGetTransactionsSuccessed(String data) {
        refreshLayout.finishRefresh();
        List<HdWalletTransaction> datas = new ArrayList<>();
        datas = new Gson().fromJson(data, new TypeToken<List<HdWalletTransaction>>() {
        }.getType());
        mData.clear();
        mData.addAll(datas);
        adapter.notifyDataSetChanged();
        if (mData.isEmpty()) {
            showPageEmpty();
        } else {
            showPageContent();
        }
    }

    @Override
    public void onGetTransactionsFailed() {
        refreshLayout.finishRefresh();
        showPageError();
    }


}
